﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BackEnd.Integration;
using BackEnd.Facade.Interfaces;

namespace BackEnd.Facade.Implementations
{
    public class MembershipManager : IMembershipManager
    {
        public Guid GetAnonymousUserId(string userName)
        {
            using (var ctx = new TaxiDataDataContext())
            {
                ctx.ObjectTrackingEnabled = false;
                return ctx.aspnet_Users.Single(m => m.UserName.Equals(userName) && m.IsAnonymous).UserId;
            }
        }

        public string GetUserNameById(Guid id)
        {
            using (var ctx = new TaxiDataDataContext())
            {
                ctx.ObjectTrackingEnabled = false;
                return ctx.aspnet_Users.Single(m => m.UserId.Equals(id)).UserName;
            }
        }

        public void PrepareToProfileDeletion(string username, string fullname)
        {
            if (String.IsNullOrEmpty(username))
            {
                throw new ArgumentException("Username can't be null or empty", "username");
            }
            using (var ctx = new TaxiDataDataContext())
            {
                var userId = GetAnonymousUserId(username);
                var feedbacks = ctx.CustomerFeedbacks.Where(m => m.UserId.Equals(userId));
               
                foreach (var feedback in feedbacks)
                {
                    feedback.UserId = null;
                    feedback.FullName = fullname;
                }
                ctx.SubmitChanges();
            }
        }
    }
}
